package com.dcy.znoa.common.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.dcy.znoa.common.enums.ResultCode;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.bean.Result;
import com.dcy.znoa.common.filter.LoginFilter;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.OperSession;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author qinkun
 */
public class BaseController {
    private static final ThreadLocal<HttpServletRequest> requestThreadLocal = new ThreadLocal<>();
    private static final ThreadLocal<HttpServletResponse> responseThreadLocal = new ThreadLocal<>();

    @ModelAttribute
    public void setRequestAndResponse(HttpServletRequest request, HttpServletResponse response)
            throws BusinessException {
        requestThreadLocal.remove();
        requestThreadLocal.set(request);
        responseThreadLocal.set(response);
    }

    public HttpServletRequest getRequest() {
        return requestThreadLocal.get();
    }

    public HttpServletResponse getResponse() {
        return responseThreadLocal.get();
    }

    public HttpSession getSession() {
        return requestThreadLocal.get().getSession();
    }


    public Oper getCurrentUser() {
        return (Oper) getRequest().getAttribute(LoginFilter.HTTP_ATTR_USER_KEY);
    }

    public OperSession getCurrentSession() {
        return (OperSession) getRequest().getAttribute(LoginFilter.HTTP_ATTR_SESSION_KEY);
    }

    public long getCurrentUserId() {
        final Oper user = getCurrentUser();
        return user == null ? -1 : user.getOperId();
    }

    public String getCurrentUsername() {
        final Oper user = getCurrentUser();
        return user == null ? null : user.getOperName();
    }

    /**
     * 成功-无返回结果
     *
     */
    protected Result success() {
        return new Result();
    }

    /**
     * 成功-有返回结果
     */
    protected Result success(Object content) {
        Result result = new Result();
        return result.setContent(content);
    }

    /**
     * 失败
     *
     * @param resultCode
     * @return
     * @author dongdong.zhang
     * @date 2015年11月24日 下午5:33:17
     */
    protected Result error(ResultCode resultCode) {
        return new Result(resultCode.code(), resultCode.message(), Result.Status.ERROR);
    }

    /**
     * 失败
     */
    protected Result error(String errorMsg) {
        return new Result(ResultCode.SERVICE_ERROR.code(), errorMsg, Result.Status.ERROR);
    }

    /**
     * 失败
     * @param errorCode
     * @param errorMsg
     * @return
     */
    protected Result error(String errorCode, String errorMsg) {
        return new Result(errorCode, errorMsg, Result.Status.ERROR);
    }

    protected JsonForList successPage(Page page) {
        return new JsonForList(page.getRecords(), page.getTotal()) ;
//        return new JsonForList(page.getRecords(), page.getRecords().size()) ;
    }
}
